﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OT.Data;
using OT.Areas.Admin.Models;
using PagedList;

namespace OT.Areas.Instructor.Controllers
{
    public class QuestionController : Controller
    {
        readonly OTEntitiesme OTDb = new OTEntitiesme();
        int LanguageID = Utilities.GetLanguageID();
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public JsonResult Index(int pageNumber, int pageSize, string searchString, int pgindex, string columnFilter)
        {
            int UserID=0;
            if (Request.Cookies["LoginSesID"]!= null)
            {
                UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
            }
            else
            {
                Response.Redirect("~/Home");
            }
            var Question = (List<Questions>)null;
            int recount = 0;
            try
            {
                Question = (from c in OTDb.Questions
                            join sc in OTDb.Subjects
                            on c.SubjectID equals sc.SubjectID into output
                            from j in output.DefaultIfEmpty()

                            join qt in OTDb.QuestionTypes
                            on c.QuestionTypeID equals qt.QuestionTypeID

                            join top in OTDb.Topics
                           on c.TopicID equals top.TopicID

                            join usr in OTDb.Users
                            on c.UserID equals usr.UserID
                            where c.LanguageID == LanguageID

                            select new Questions
                            {
                                QuestionID = (int)c.QuestionID,
                                QuestionName = c.QuestionName,
                                QuestionType1 = qt.QuestionType1,
                                SubjectName = j.SubjectName,
                                CategoryName=j.Category.CategoryName,
                                FirstName = usr.FirstName,
                                TopicName = top.TopicName,
                                CreatedDate = (DateTime)c.CreatedDate,
                                UpdatedDate = (DateTime)c.UpdatedDate,
                            }).ToList();

                if (!String.IsNullOrEmpty(searchString))
                    Question = Question.Where(r => r.QuestionName.ToLower().Contains(searchString.ToLower())).ToList();
                   
                recount =Question.Count;

                Question = Question.Skip<Questions>(pgindex * (pageNumber - 1))
                    .Take<Questions>(pgindex).ToList<Questions>();


            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return Json(new
            {
                PageNumber = pageNumber,
                PageSize = pageSize,
                PgIndex = pgindex,
                Data = Question,
                RecordCount = recount
            });
        }

        public ActionResult Create()
        {
            Questions Q = null;
            try
            {
                Q = new Questions();
               
                var cat = OTDb.Categories.Where(c=>c.Status==true).ToList();
                Q.catgo = cat;
                var QT1 = OTDb.QuestionTypes.ToList();
                Q.QuestionTypes = QT1;
                var Sub = OTDb.Subjects.Where(s=>s.Status==true).ToList();
                Q.Subjects = Sub;
                var top = OTDb.Topics.Where(t=>t.Status==true).ToList();
                Q.Topics = top;
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(Q);
        }
        [HttpPost]
        public string Create(string QuestionName, int QuestionTypeID, int SubjectID, int TopicID, string Answer1, string Answer2, string Answer3, string Answer4, byte DisplayAnswer, byte CorrectAnswer, bool UseLatex, bool Status)
        {
            string result = "";
            Question Qust = null;
            try
            {
                if (ModelState.IsValid)
                {
                    int UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
                    Qust = new Question();
                    Qust.QuestionName = QuestionName;
                    if (QuestionTypeID != 0)
                        Qust.QuestionTypeID = QuestionTypeID;
                    if (SubjectID != 0)
                        Qust.SubjectID = SubjectID;
                    if (TopicID != 0)
                        Qust.TopicID = TopicID;
                    Qust.UserID = UserID;
                    Qust.Answer1 =Answer1;
                    Qust.Answer2 = Answer2;
                    Qust.Answer3 = Answer3;
                    Qust.Answer4 = Answer4;
                    Qust.DisplayAnswer = DisplayAnswer;
                    Qust.CorrectAnswer = CorrectAnswer;
                    Qust.UseLatex = UseLatex;
                    Qust.Status = Status;
                    Qust.LanguageID = LanguageID;
                    Qust.CreatedDate = DateTime.Now;
                    Qust.UpdatedDate = DateTime.Now;
                    OTDb.Questions.Add(Qust);
                    OTDb.SaveChanges();
                    result = "Sucess";
                }
                else
                {
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;

        }
        public ActionResult Edit(int QuestionID)
        {
            Questions Quest = null;
            try
            {
                var Question = OTDb.Questions.Where(r => r.QuestionID == QuestionID).SingleOrDefault();
                if (Question != null)
                {
                   
                    Quest = new Questions();
                    var QT1 = OTDb.QuestionTypes.ToList();
                    Quest.QuestionTypes = QT1;
                    var Sub = OTDb.Subjects.Where(p => p.SubjectID == Question.SubjectID).ToList();
                    Quest.Subjects = Sub;
                    var top = OTDb.Topics.Where(p => p.TopicID == Question.TopicID).ToList();
                    Quest.Topics = top;
                    var cat = OTDb.Categories.ToList();
                    Quest.catgo = cat;
                    Quest.QuestionID = Question.QuestionID;
                    Quest.QuestionName = Question.QuestionName;
                    var cate = OTDb.Subjects.Where(r => r.SubjectID == Question.SubjectID).SingleOrDefault();
                    if(cate !=null)
                        Quest.CategoryID = cate.CategoryID;
                    Quest.SubjectID = Question.SubjectID;
                    Quest.TopicID = Question.TopicID;
                    Quest.Answer1 = Question.Answer1;
                    Quest.Answer2 = Question.Answer2;
                    Quest.Answer3 = Question.Answer3;
                    Quest.Answer4 = Question.Answer4;
                    Quest.DisplayAnswer = (byte)Question.DisplayAnswer;
                    Quest.CorrectAnswer = (byte)Question.CorrectAnswer;
                    Quest.UseLatex = (bool)Question.UseLatex;
                    Quest.Status = (bool)Question.Status;
                    if (Question.QuestionTypeID != null)
                        Quest.QuestionTypeID = (int)Question.QuestionTypeID;
                    if (Question.SubjectID != null)
                        Quest.SubjectID = (int)Question.SubjectID;
                    if (Question.TopicID != null)
                        Quest.TopicID = (int)Question.TopicID;
                    
                  
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(Quest);
        }
        [HttpPost]
        public string Edit(int QuestionID, string QuestionName, int QuestionTypeID, int SubjectID, int TopicID, string Answer1, string Answer2, string Answer3, string Answer4, byte DisplayAnswer, byte CorrectAnswer, bool UseLatex, int CategoryID)
        {
            string result = "";
            try
            {
                if (ModelState.IsValid)
                {
                    var Quest = OTDb.Questions.Where(r => r.QuestionID == QuestionID).SingleOrDefault();
                    if (Quest != null)
                    {
                        int UserID = Convert.ToInt16(Request.Cookies["LoginSesID"].Value);
                        Quest.QuestionName = QuestionName;
                        Quest.Answer1 = Answer1;
                        Quest.Answer2 = Answer2;
                        Quest.Answer3=Answer3;
                        Quest.Answer4 =Answer4;
                        Quest.DisplayAnswer = (byte)DisplayAnswer;
                        Quest.CorrectAnswer = (byte)CorrectAnswer;
                        Quest.UseLatex = (bool)UseLatex;
                        Quest.UpdatedDate = DateTime.Now;
                        Quest.QuestionTypeID = (int)QuestionTypeID;
                        Quest.SubjectID = (int)SubjectID;
                        Quest.TopicID = (int)TopicID;
                        Quest.UserID = UserID;
                        OTDb.SaveChanges();
                        result = "Sucess";
                    }
                }
                else
                {
                    result = "NotValid";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return result;
        }

        public ActionResult Delete(int QuestionID)
        {
            Questions Quest = null;
            try
            {
                var Question = OTDb.Questions.Find(QuestionID);
                if (Question != null)
                {
                    Quest = new Questions();
                    Quest.QuestionID = Question.QuestionID;
                    Quest.QuestionName = Question.QuestionName;
                    Quest.Answer1 = Question.Answer1;
                    Quest.Answer2 = Question.Answer2;
                    Quest.Answer3 = Question.Answer3;
                    Quest.Answer4 = Question.Answer4;
                    Quest.DisplayAnswer = (byte)Question.DisplayAnswer;
                    Quest.CorrectAnswer = (byte)Question.CorrectAnswer;
                    Quest.UseLatex = (bool)Question.UseLatex;
                    Quest.Status = (bool)Question.Status;
                    if (Question.QuestionTypeID != null)
                        Quest.QuestionTypeID = (int)Question.QuestionTypeID;
                    if (Question.SubjectID != null)
                        Quest.SubjectID = (int)Question.SubjectID;
                    if (Question.TopicID != null)
                        Quest.TopicID = (int)Question.TopicID;
                    var QT1 = OTDb.QuestionTypes.ToList();
                    Quest.QuestionTypes = QT1;
                    var Sub = OTDb.Subjects.ToList();
                    Quest.Subjects = Sub;
                    var top = OTDb.Topics.ToList();
                    Quest.Topics = top;
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
            }
            return PartialView(Quest);
        }
        private IList<Subject> GetSub(int CategoryID)
        {
            return OTDb.Subjects.Where(m => m.CategoryID == CategoryID).ToList();
        }
        [AcceptVerbs(HttpVerbs.Get)]
        public JsonResult LoadSubject(string CategoryID)
        {
            var stateList = this.GetSub(Convert.ToInt32(CategoryID));
            var stateData = stateList.Select(m => new SelectListItem()
            {
                Text = m.SubjectName,
                Value = m.SubjectID.ToString(),
            });
            return Json(stateData, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public string Delete(int QuestionID, string IsDelete)
        {
            string result = "";
            try
            {
                var Question = OTDb.Questions.Where(r => r.QuestionID == QuestionID).SingleOrDefault();
                if (Question != null)
                {
                    OTDb.Questions.Remove(Question);
                    OTDb.SaveChanges();
                    TempData["delete"] = "Successfully Delete";
                    result = "Sucess";
                }
            }
            catch (Exception ex)
            {
                Utilities.InsertErrorLog(ex);
                result = "NotValid";
            }
            return result;
        }
    }
}
